<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>STL模板详解 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/27.13d60edd.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/149.ffc835b5.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/20.b5af7fad.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/26.74ba4989.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/42.76f52d62.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/57.a9caec0d.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/6.8c2ea393.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/72.e5467385.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link router-link-active">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link router-link-active">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>STL模板详解</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#📑-目录" class="sidebar-link">📑 目录</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_01-stl优点和注意事项" class="sidebar-link">01.STL优点和注意事项</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_02-stl训练习题集" class="sidebar-link">02.STL训练习题集</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-1-vector（单端数组）" class="sidebar-link">2.1.vector（单端数组）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-2-list（双向链表）" class="sidebar-link">2.2.list（双向链表）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-3-set『3种』" class="sidebar-link">2.3.set『3种』</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-4-map『3种』" class="sidebar-link">2.4.map『3种』</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-5-string" class="sidebar-link">2.5.string</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-6-queue『3种』" class="sidebar-link">2.6.queue『3种』</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-7-stack" class="sidebar-link">2.7.stack</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-8-pair" class="sidebar-link">2.8.pair</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_2-9-bitset『比特集合』" class="sidebar-link">2.9.bitset『比特集合』</a></li></ul></li><li><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_03-stl增删改查（crud）" class="sidebar-link">03.STL增删改查（CRUD）</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️3-0-字符串函数的记忆口诀" class="sidebar-link">⭐️3.0.字符串函数的记忆口诀</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️3-1-string字符串『重要』" class="sidebar-link">⭐️3.1.string字符串『重要』</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-1-1-others成员函数" class="sidebar-link">3.1.1.others成员函数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-1-2-『增』元素" class="sidebar-link">3.1.2.『增』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-1-3『删』元素" class="sidebar-link">3.1.3『删』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-1-4『改』元素" class="sidebar-link">3.1.4『改』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-1-5-『查』元素" class="sidebar-link">3.1.5.『查』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️3-2-list双向链表" class="sidebar-link">⭐️3.2.list双向链表</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-2-1-others成员函数" class="sidebar-link">3.2.1.others成员函数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-2-2-『增』元素" class="sidebar-link">3.2.2.『增』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-2-3-『删』元素" class="sidebar-link">3.2.3.『删』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-2-4-『改』元素" class="sidebar-link">3.2.4.『改』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-2-5-『查』元素" class="sidebar-link">3.2.5.『查』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️3-3-优先队列priority-queue" class="sidebar-link">⭐️3.3.优先队列priority_queue</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-3-0-大小顶堆" class="sidebar-link">3.3.0.大小顶堆</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-3-1-『增』元素" class="sidebar-link">3.3.1.『增』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-3-2-『删』元素" class="sidebar-link">3.3.2.『删』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-3-3-『改』元素" class="sidebar-link">3.3.3.『改』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-3-4-『查』元素" class="sidebar-link">3.3.4.『查』元素</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_3-3-5-『元素个数』" class="sidebar-link">3.3.5.『元素个数』</a></li></ul></li><li><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#✔️04-仿函数统一场理论" class="sidebar-link">✔️04.仿函数统一场理论</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_4-1-「自建函数」和「比较类-仿函数-」的统一场理论" class="sidebar-link">4.1.「自建函数」和「比较类(仿函数)」的统一场理论</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_4-2-应用：自建比较priority-queue和sort" class="sidebar-link">4.2.应用：自建比较priority_queue和sort</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#✔️4-3-priority-queue" class="sidebar-link">✔️4.3.priority_queue</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️4-3-1-难点（没搞懂）" class="sidebar-link">⭐️4.3.1.难点（没搞懂）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️4-3-2-基本数据类型" class="sidebar-link">⭐️4.3.2.基本数据类型</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️4-3-3-结构体类型（重载-）" class="sidebar-link">⭐️4.3.3.结构体类型（重载&lt;）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️4-3-3-1-方法一" class="sidebar-link">⭐️4.3.3.1.方法一</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#⭐️4-3-3-2-方法二（比较类）" class="sidebar-link">⭐️4.3.3.2.方法二（比较类）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_4-4-sort函数" class="sidebar-link">4.4.sort函数</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_4-4-1-尝试统一sort和-priority-queue" class="sidebar-link">4.4.1.尝试统一sort和 priority_queue</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_4-4-2-方法（建比较函数）" class="sidebar-link">4.4.2.方法（建比较函数）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_4-5-stl中可以放仿函数的地方也可以放普通函数？" class="sidebar-link">4.5.STL中可以放仿函数的地方也可以放普通函数？</a></li></ul></li><li><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_05-string剖析c" class="sidebar-link">05.string剖析C++</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_5-1源代码来源" class="sidebar-link">5.1源代码来源</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_5-2string的使用接口" class="sidebar-link">5.2string的使用接口</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#_5-3bits-basic-string-h头文件" class="sidebar-link">5.3bits/basic_string.h头文件</a></li></ul></li><li><a href="/01.Language/Cpp/03.STL%E6%A8%A1%E6%9D%BF%E8%AF%A6%E8%A7%A3.html#参考资料" class="sidebar-link">参考资料</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="stl模板详解"><a href="#stl模板详解" class="header-anchor">#</a> STL模板详解</h1> <h2 id="📑-目录"><a href="#📑-目录" class="header-anchor">#</a> 📑 目录</h2> <p>[TOC]</p> <h2 id="_01-stl优点和注意事项"><a href="#_01-stl优点和注意事项" class="header-anchor">#</a> 01.STL优点和注意事项</h2> <ul><li>数据结构学习建议
<ul><li><p>不要拿C语言学数据结构</p></li> <li><p>不要用纯C语言刷算法题</p></li></ul></li> <li>C++中STL是学习数据结构和算法的利器</li></ul> <p>请使用<strong>C++11编译选项</strong>编译！！</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>-std=c++11
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li><p>STL不熟悉的教训</p> <ul><li>1、STL不熟，什么都容易变成用C语言的想法，写代码就要自力更生。比如，字符串匹配，就想着自己写，却不知道用string。
<ul><li>『强烈建议』：要把STL弄得炉火纯青再去刷题！</li></ul></li> <li>2、能用STL的，除非极限的效率，不然，绝对不自己写！！
其实这也是阻碍我以前一直没有快速进步的原因！</li> <li>3、本来算法应该重点在如何进行。而不是代码如何实现，
但是由于当时我自己的代码实现水平的太差，搞得，其实我有其他想法，
但是算法的时候，我就又改为C语言风格的了。代码量冗长，对比赛和秋季招聘『笔试』极其不利！</li> <li>4、我在某厂面试的时候，由于记错STL中string的成员函数...debug了一段时间，然后挂了。</li></ul></li> <li><p>STL的注意事项</p></li></ul> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>面试时候的，可能要你手写『堆排序』，那么你就不准用STL的make_heap和sort_heap来解答
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_02-stl训练习题集"><a href="#_02-stl训练习题集" class="header-anchor">#</a> 02.STL训练习题集</h2> <ul><li>类模板</li> <li>函数模板</li> <li>数据结构</li> <li>算法</li> <li>封装</li></ul> <blockquote><p>1、数据结构类</p></blockquote> <h3 id="_2-1-vector（单端数组）"><a href="#_2-1-vector（单端数组）" class="header-anchor">#</a> 2.1.vector（单端数组）</h3> <ul><li><strong>单端数组</strong>/向量/可变长数组</li></ul> <p><code>#include&lt;vector&gt;</code></p> <p>可变长数组
底层实现是数组
具体可以看清华邓俊辉的</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>**单端数组**/向量/可变长数组
erase函数
26. 删除排序数组中的重复项
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>2.1.1.vector类型常用函数</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token function">copy</span><span class="token punctuation">(</span>solve<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> solve<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> flag<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//将solve数组中的数据，拷贝到flag.begin()+3的位置开始</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> solve<span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">temp</span><span class="token punctuation">(</span>solve<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>solve<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">demo</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>a<span class="token operator">+</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

demo<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">mySolve</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-2-list（双向链表）"><a href="#_2-2-list（双向链表）" class="header-anchor">#</a> 2.2.list（双向链表）</h3> <ul><li>双向链表</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;list&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-3-set『3种』"><a href="#_2-3-set『3种』" class="header-anchor">#</a> 2.3.set『3种』</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;set&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>set（集合-有序）</p> <p>集合
内部自动有序
并且不含重复元素的容器，也就是会去重。</p> <p>内部实现是<strong>红黑树</strong></p> <h5 id="_1、multiset『多重集合-有序』"><a href="#_1、multiset『多重集合-有序』" class="header-anchor">#</a> <strong>1、multiset</strong>『多重集合-有序』</h5> <blockquote><p>set中元素是唯一的。
而我的这个的元素不是唯一的。
也就是元素没有去重。</p></blockquote> <p>样例：</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>输入一个长度不超过20的字符串，对所输入的字符串，
按照ASCII码的大小从小到大进行排序，请输出排序后的结果
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;set&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
 
 
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">char</span> str<span class="token punctuation">[</span><span class="token number">20</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    multiset<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token operator">&gt;</span> st<span class="token punctuation">;</span><span class="token comment">//要是用set，则去重了，不行baca这样子的</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">~</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%s&quot;</span><span class="token punctuation">,</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        st<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
         
        <span class="token keyword">while</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">!=</span><span class="token string">'\0'</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            st<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span>str<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token operator">++</span>i<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
         
        <span class="token keyword">for</span><span class="token punctuation">(</span>multiset<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token operator">&gt;</span><span class="token operator">::</span>iterator it<span class="token operator">=</span>st<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">;</span> it<span class="token operator">!=</span>st<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">;</span>it<span class="token operator">++</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c&quot;</span><span class="token punctuation">,</span><span class="token operator">*</span>it<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
         
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">memset</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
     
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><h5 id="_2、unordered-set『无序集合』"><a href="#_2、unordered-set『无序集合』" class="header-anchor">#</a> <strong>2、unordered_set</strong>『无序集合』</h5> <ul><li><font color="red">C++11标准中添加的</font></li></ul> <blockquote><p>可以用来处理只去重复，但是不排序
内部实现是散列（hash）</p></blockquote> <h3 id="_2-4-map『3种』"><a href="#_2-4-map『3种』" class="header-anchor">#</a> 2.4.map『3种』</h3> <ul><li>映射-有序</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;map&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>映射</p> <p>可以将任何基本类型和STL容器，映射到任何基本类型和STL容器</p> <p>注意，map中的键和值是唯一的</p> <p>内部实现是<strong>红黑树</strong></p> <h5 id="_1、multimap『映射-有序』"><a href="#_1、multimap『映射-有序』" class="header-anchor">#</a> <strong>1、multimap</strong>『映射-有序』</h5> <blockquote><p>一个键对应多个值</p></blockquote> <h5 id="_2、unordered-map『无序映射』"><a href="#_2、unordered-map『无序映射』" class="header-anchor">#</a> <strong>2、unordered_map</strong>『无序映射』</h5> <ul><li><font color="red">C++11标准中添加的</font></li></ul> <blockquote><p>底层实现是散列（hash）</p></blockquote> <h4 id="unordered-map的『经典用法』题目"><a href="#unordered-map的『经典用法』题目" class="header-anchor">#</a> unordered_map的『经典用法』题目</h4> <p>1、两数之和</p> <p><a href="https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f?tpId=191&&tqId=36133&rp=1&ru=/ta/job-code-high-algorithm&qru=/ta/job-code-high-algorithm/question-ranking" target="_blank" rel="noopener noreferrer">两数之和<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p><strong>注意点</strong>：</p> <p>1）unordered_map的find要是找不到返回的东西 mp.end()<br>
2）map的键和值是唯一的，所以本题显然是不能用的。<br>
3）multimap可以一个键对应多个值，unorder_map也是一样。</p> <ul><li>区别，mulimap不仅映射并且按key排序的需求</li> <li>unorder_map只映射，不按key(键)排序。</li></ul> <p>所以，我们才可以将比如 3 2 2 3 寻找6<br>
将3映射为0，将3映射为3，这样来解题</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//奇怪的是，为什么这题中，我想要用unordered_map还用亲自弄头文件</span>
<span class="token comment">//当然显然#include &lt;unordered_map&gt;也是可以的</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>

<span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token comment">/**
     * 
     * @param numbers int整型vector 
     * @param target int整型 
     * @return int整型vector
     */</span>
    vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">twoSum</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> numbers<span class="token punctuation">,</span> <span class="token keyword">int</span> target<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// write code here</span>
        unordered_map<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span><span class="token keyword">int</span><span class="token operator">&gt;</span> mp<span class="token punctuation">;</span>
        
        <span class="token keyword">int</span> len<span class="token operator">=</span>numbers<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>len <span class="token punctuation">;</span> <span class="token operator">++</span>i <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            mp<span class="token punctuation">[</span> numbers<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token punctuation">]</span><span class="token operator">=</span>i<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>len <span class="token punctuation">;</span> <span class="token operator">++</span>i <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">int</span> num<span class="token operator">=</span>target<span class="token operator">-</span>numbers<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
            
            <span class="token comment">//如果num存在！！！并且不是nums[i]本身</span>
            <span class="token keyword">if</span><span class="token punctuation">(</span> mp<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token operator">!=</span> mp<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> mp<span class="token punctuation">[</span>num<span class="token punctuation">]</span><span class="token operator">!=</span>i <span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token keyword">int</span> a<span class="token operator">=</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
                <span class="token keyword">int</span> b<span class="token operator">=</span>mp<span class="token punctuation">[</span>num<span class="token punctuation">]</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">;</span>
                vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> rt<span class="token punctuation">;</span>
                rt<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">;</span>
                rt<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> rt<span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
        
        
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br></div></div><h3 id="_2-5-string"><a href="#_2-5-string" class="header-anchor">#</a> 2.5.string</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-6-queue『3种』"><a href="#_2-6-queue『3种』" class="header-anchor">#</a> 2.6.queue『3种』</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;queue&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>队列</p> <h5 id="_2-6-1-deque（双端数组）"><a href="#_2-6-1-deque（双端数组）" class="header-anchor">#</a> 2.6.1.deque（双端数组）</h5> <ul><li>双端队列/双向队列</li></ul> <h5 id="_2-6-2-priority-queue（优先队列-堆）"><a href="#_2-6-2-priority-queue（优先队列-堆）" class="header-anchor">#</a> 2.6.2.priority_queue（优先队列/堆）</h5> <ul><li><p><code>include&lt;queue&gt;</code>就可以用优先队列</p></li> <li><p>优先队列</p></li> <li><p>用堆实现的<strong>默认</strong>将当前队列<strong>最大元素</strong>置于队首的容器</p></li> <li><p>请用<code>priority_queue</code>设置最小堆写</p></li> <li><p>用<code>make_heap</code>设置最小堆写</p> <ul><li><a href="https://www.nowcoder.com/questionTerminal/3eef8d66b0fa4f71a8498974547fe670" target="_blank" rel="noopener noreferrer">腾讯笔试-石子合并<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li> <li><a href="https://leetcode-cn.com/problems/shu-ju-liu-zhong-de-zhong-wei-shu-lcof/" target="_blank" rel="noopener noreferrer">剑指 Offer 41. 数据流中的中位数<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>『维护大顶堆和小顶堆』</li> <li><a href="https://leetcode-cn.com/problems/kth-largest-element-in-a-stream/" target="_blank" rel="noopener noreferrer">703. 数据流中的第 K 大元素<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>『动态维护小顶堆』</li></ul></li></ul> <h3 id="_2-7-stack"><a href="#_2-7-stack" class="header-anchor">#</a> 2.7.stack</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stack&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_2-8-pair"><a href="#_2-8-pair" class="header-anchor">#</a> 2.8.pair</h3> <ul><li><p>（使）成对</p></li> <li><p><code>#include&lt;utility&gt;</code>才能使用（奇奇怪怪）</p></li> <li><p>注意，map内部实现涉及到了pair,所以，要是我们</p></li> <li><p><code>#include&lt;map&gt;</code></p></li> <li><p>那么我们要是想要使用pair，就不用另外<code>#include&lt;utility&gt;</code>了</p></li></ul> <h3 id="_2-9-bitset『比特集合』"><a href="#_2-9-bitset『比特集合』" class="header-anchor">#</a> 2.9.bitset『比特集合』</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bitset&gt;</span></span>

头⽂件是 #include <span class="token operator">&lt;</span>bitset<span class="token operator">&gt;</span> ，bitset可能在PAT、蓝桥OJ中不常⽤

但是在LeetCode OJ中经常⽤到～⽽且知道bitset能够简化⼀些操作，可能⼀些复杂的问题能够
直接⽤bitset就很轻易地解决～

以下是⼀些常⽤⽤法
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>若干个位</p> <blockquote><p>2、算法类头文件</p></blockquote> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;algorithm&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_03-stl增删改查（crud）"><a href="#_03-stl增删改查（crud）" class="header-anchor">#</a> 03.STL增删改查（CRUD）</h2> <h3 id="⭐️3-0-字符串函数的记忆口诀"><a href="#⭐️3-0-字符串函数的记忆口诀" class="header-anchor">#</a> ⭐️3.0.字符串函数的记忆口诀</h3> <ul><li>Create(创建)</li> <li>Read Retrieve(读取)</li> <li>Update（更新）</li> <li>Delete（删除）</li></ul> <h3 id="⭐️3-1-string字符串『重要』"><a href="#⭐️3-1-string字符串『重要』" class="header-anchor">#</a> ⭐️3.1.<code>string</code>字符串『重要』</h3> <ul><li>注意，这个类的底层有个静态变量<code>string::npos</code></li> <li><code>npos</code> is a static member constant value with the greatest possible value for an element of member type <code>size_type</code>.</li></ul> <h3 id="_3-1-1-others成员函数"><a href="#_3-1-1-others成员函数" class="header-anchor">#</a> 3.1.1.others成员函数</h3> <ul><li>1、构造函数</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>『默认构造』
    <span class="token function">string</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
『拷贝构造』
    <span class="token function">string</span><span class="token punctuation">(</span><span class="token keyword">const</span> string <span class="token operator">&amp;</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//比如string strDst(str)</span>
	<span class="token function">string</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>s<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//比如string strDst( solve )</span>
	<span class="token function">string</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">char</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>2、赋值</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>『重载赋值运算符』 
    string <span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> s <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//str=s;</span>

『assign函数』
	string <span class="token operator">&amp;</span> <span class="token function">assign</span><span class="token punctuation">(</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//str.assign(s);</span>
	string <span class="token operator">&amp;</span> <span class="token function">assign</span><span class="token punctuation">(</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span>
	string <span class="token operator">&amp;</span> <span class="token function">assign</span><span class="token punctuation">(</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> s <span class="token punctuation">)</span><span class="token punctuation">;</span>
	string <span class="token operator">&amp;</span> <span class="token function">assign</span><span class="token punctuation">(</span> <span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">char</span> c <span class="token punctuation">)</span><span class="token punctuation">;</span>
	string <span class="token operator">&amp;</span> <span class="token function">assign</span><span class="token punctuation">(</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> start<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li>3、长度</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>	<span class="token comment">//长度不包括字符串末尾的'\0'</span>
<span class="token keyword">bool</span> <span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>	<span class="token comment">//当前字符串是否为空</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>4、拷贝『string→字符数组』</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> <span class="token function">copy</span><span class="token punctuation">(</span> <span class="token keyword">char</span> <span class="token operator">*</span> Dst<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">int</span> pos<span class="token operator">=</span><span class="token number">0</span> <span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>

<span class="token comment">//str.copy( dst, str.size() , 0 );</span>
<span class="token comment">//str.copy( dst, str.size() );</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> <span class="token function">c_str</span><span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span><span class="token comment">//返回1个以'\0'结尾的字符串的首地址</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-1-2-『增』元素"><a href="#_3-1-2-『增』元素" class="header-anchor">#</a> 3.1.2.『增』元素</h3> <ul><li>1、字符串连接</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>运算符重载：
    string <span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">+=</span><span class="token punctuation">(</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> S<span class="token punctuation">)</span><span class="token punctuation">;</span>
	string <span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">+=</span><span class="token punctuation">(</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span>

append单词
	string <span class="token operator">&amp;</span> <span class="token function">append</span><span class="token punctuation">(</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s <span class="token punctuation">)</span><span class="token punctuation">;</span> 
	
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="_3-1-3『删』元素"><a href="#_3-1-3『删』元素" class="header-anchor">#</a> 3.1.3『删』元素</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//删除从postion开始的n个字符，返回修改后的字符串</span>
string <span class="token operator">&amp;</span> <span class="token function">erase</span><span class="token punctuation">(</span> <span class="token keyword">int</span> pos<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">int</span> Len<span class="token operator">=</span>npos <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_3-1-4『改』元素"><a href="#_3-1-4『改』元素" class="header-anchor">#</a> 3.1.4『改』元素</h3> <ul><li>1、插入元素</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>string <span class="token operator">&amp;</span> <span class="token function">insert</span><span class="token punctuation">(</span> <span class="token keyword">int</span> pos<span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s <span class="token punctuation">)</span><span class="token punctuation">;</span>
string <span class="token operator">&amp;</span> <span class="token function">insert</span><span class="token punctuation">(</span> <span class="token keyword">int</span> pos<span class="token punctuation">,</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> S<span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>2、替换元素</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//删除从pos开始的n个字符，然后在pos处插入s</span>
string <span class="token operator">&amp;</span> <span class="token function">replace</span><span class="token punctuation">(</span> <span class="token keyword">int</span> pos<span class="token punctuation">,</span> <span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span>
string <span class="token operator">&amp;</span> <span class="token function">replace</span><span class="token punctuation">(</span> <span class="token keyword">int</span> pos， <span class="token keyword">int</span> n<span class="token punctuation">,</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> S<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li><font style="background:yellow;">记忆方法</font>：<code>替换=先删掉(erase)然后插入</code>，所以才<code>int pos, int n, const char * s</code></li> <li>3、交换string</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">void</span> <span class="token function">swap</span><span class="token punctuation">(</span>string <span class="token operator">&amp;</span> s2<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-1-5-『查』元素"><a href="#_3-1-5-『查』元素" class="header-anchor">#</a> 3.1.5.『查』元素</h3> <ul><li>1、从前往后和从后往前查找</li> <li><font style="background:yellow;">记忆方法</font>：我觉得这是『string代码』设计接口中，唯一的一个Bug，因为<code>const char * s</code>在前面</li> <li><font style="background:yellow;">记忆口诀</font>：string查，position放后『“查于片刻后”，茶余片刻后』</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//找不到就返回-1</span>
<span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span> <span class="token keyword">char</span> c<span class="token punctuation">,</span> <span class="token keyword">int</span> pos<span class="token operator">=</span><span class="token number">0</span> <span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> pos<span class="token operator">=</span><span class="token number">0</span> <span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">find</span><span class="token punctuation">(</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> S<span class="token punctuation">,</span> <span class="token keyword">int</span> pos<span class="token operator">=</span><span class="token number">0</span> <span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>


<span class="token comment">//从后往前找</span>
<span class="token keyword">int</span> <span class="token function">rfind</span><span class="token punctuation">(</span> <span class="token keyword">char</span> c<span class="token punctuation">,</span> <span class="token keyword">int</span> pos<span class="token operator">=</span>npos <span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">rfind</span><span class="token punctuation">(</span> <span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> s<span class="token punctuation">,</span> <span class="token keyword">int</span> pos<span class="token operator">=</span>npos <span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token function">rfind</span><span class="token punctuation">(</span> <span class="token keyword">const</span> string <span class="token operator">&amp;</span> S<span class="token punctuation">,</span> <span class="token keyword">int</span> pos<span class="token operator">=</span>npos <span class="token punctuation">)</span><span class="token keyword">const</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><ul><li>2、获取string的子串</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>string <span class="token function">substr</span> <span class="token punctuation">(</span><span class="token keyword">int</span> pos <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">int</span> Len <span class="token operator">=</span> npos<span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="⭐️3-2-list双向链表"><a href="#⭐️3-2-list双向链表" class="header-anchor">#</a> ⭐️3.2.<code>list</code>双向链表</h3> <h3 id="_3-2-1-others成员函数"><a href="#_3-2-1-others成员函数" class="header-anchor">#</a> 3.2.1.others成员函数</h3> <ul><li>1、构造函数</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>list<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> mylist<span class="token punctuation">;</span>
list<span class="token operator">&lt;</span>node <span class="token operator">*</span><span class="token operator">&gt;</span> mylist<span class="token punctuation">;</span>
list<span class="token operator">&lt;</span> pair<span class="token operator">&lt;</span><span class="token keyword">int</span>， vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">::</span>iterator <span class="token operator">&gt;</span> <span class="token operator">&gt;</span> mylist<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>2、赋值</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>list <span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span><span class="token punctuation">(</span> <span class="token keyword">const</span> list <span class="token operator">&amp;</span> Lst <span class="token punctuation">)</span><span class="token punctuation">;</span>

MyList<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span> beg <span class="token punctuation">,</span> end <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//拷贝[beg,end)区间的数据</span>
MyList<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span> n<span class="token punctuation">,</span> elem <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>3、长度获取和修改</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>list<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
list<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

list<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span> num <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//变长，用默认值0填充，变短，尾部的元素被删掉</span>
list<span class="token punctuation">.</span><span class="token function">resize</span><span class="token punctuation">(</span> num <span class="token punctuation">,</span>elem <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//用num而不是0填充</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>反转链表『233，想起，面试题』</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>mylist<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-2-2-『增』元素"><a href="#_3-2-2-『增』元素" class="header-anchor">#</a> 3.2.2.『增』元素</h3> <ul><li>1、使用push之类的</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>list<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span> elem <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//尾巴增加</span>
list<span class="token punctuation">.</span><span class="token function">push_front</span><span class="token punctuation">(</span> elem <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//前面增加，这叫“双向”之一</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>2、使用insert之类的</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>MyList<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span> pos<span class="token punctuation">,</span> elem <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//在pos位置插入1个elem元素的拷贝，返回新数据的位置</span>
MyList<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span> pos<span class="token punctuation">,</span> n<span class="token punctuation">,</span> elem <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//插入n个元素，无返回值</span>
MyList<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span> pos<span class="token punctuation">,</span> beg<span class="token punctuation">,</span> end <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_3-2-3-『删』元素"><a href="#_3-2-3-『删』元素" class="header-anchor">#</a> 3.2.3.『删』元素</h3> <ul><li>1、pop一类的</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>list<span class="token punctuation">.</span><span class="token function">pop_back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//删掉尾巴1个</span>
list<span class="token punctuation">.</span><span class="token function">pop_front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>MyList<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span> beg<span class="token punctuation">,</span> end <span class="token punctuation">)</span><span class="token punctuation">;</span>
MyList<span class="token punctuation">.</span><span class="token function">erase</span><span class="token punctuation">(</span> pos <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//删掉，容器中『所有与elem』值相匹配的元素</span>
MyList<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span> elem <span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//移除，整个容器中的所有数据</span>
MyList<span class="token punctuation">.</span><span class="token function">clear</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_3-2-4-『改』元素"><a href="#_3-2-4-『改』元素" class="header-anchor">#</a> 3.2.4.『改』元素</h3> <h3 id="_3-2-5-『查』元素"><a href="#_3-2-5-『查』元素" class="header-anchor">#</a> 3.2.5.『查』元素</h3> <ul><li>1、查前面和后面</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> num<span class="token operator">=</span>list<span class="token punctuation">.</span><span class="token function">front</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> num<span class="token operator">=</span>list<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//注意是back，不是end!!! </span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="⭐️3-3-优先队列priority-queue"><a href="#⭐️3-3-优先队列priority-queue" class="header-anchor">#</a> ⭐️3.3.优先队列<code>priority_queue</code></h3> <ul><li><p>我常常把它当做大顶堆来用</p></li> <li><p><a href="https://leetcode-cn.com/problems/shu-ju-liu-zhong-de-zhong-wei-shu-lcof/" target="_blank" rel="noopener noreferrer">剑指 Offer 41. 数据流中的中位数<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li> <li><p>参考<a href="https://leetcode-cn.com/problems/find-median-from-data-stream/solution/feng-xian-dui-chong-dui-ding-dui-qiu-zho-a36j/" target="_blank" rel="noopener noreferrer">思路<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">MedianFinder</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    <span class="token comment">/** initialize your data structure here. */</span>
    <span class="token function">MedianFinder</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        count<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span><span class="token comment">//初始化元素个数</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">void</span> <span class="token function">addNum</span><span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//特判</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span> downBigHeap<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> 
        <span class="token punctuation">{</span>
            downBigHeap<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token operator">++</span>count<span class="token punctuation">;</span>
            <span class="token keyword">return</span> <span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span> num<span class="token operator">&lt;=</span>downBigHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span> 
        <span class="token punctuation">{</span>
            downBigHeap<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            upSmallHeap<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//下面开始调整2个堆的元素个数差不多数目</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span> downBigHeap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span>upSmallHeap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span> <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            upSmallHeap<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span> downBigHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
            downBigHeap<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token keyword">if</span><span class="token punctuation">(</span> downBigHeap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token operator">&lt;</span>upSmallHeap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            
            downBigHeap<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span> upSmallHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
            upSmallHeap<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//当然要记数字</span>
        <span class="token operator">++</span>count<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">double</span> <span class="token function">findMedian</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token number">0</span><span class="token operator">==</span>count <span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token number">0.0</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span> count <span class="token operator">&amp;</span><span class="token number">1</span> <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">return</span> 
            downBigHeap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">&gt;</span>upSmallHeap<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span> downBigHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">:</span> upSmallHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">else</span>
        <span class="token punctuation">{</span>
            <span class="token comment">//灵感来自『二分查找』中注意事项</span>
            <span class="token keyword">return</span> <span class="token punctuation">(</span> upSmallHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span>downBigHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">0.5</span><span class="token operator">+</span>downBigHeap<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token keyword">private</span><span class="token operator">:</span>
    <span class="token comment">//存一组小元素的，大顶堆</span>
    priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> downBigHeap<span class="token punctuation">;</span>
    <span class="token comment">//存一组大元素的，小顶堆</span>
    priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token punctuation">,</span> greater<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> upSmallHeap<span class="token punctuation">;</span>
    <span class="token keyword">int</span> count<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">/**
 * Your MedianFinder object will be instantiated and called as such:
 * MedianFinder* obj = new MedianFinder();
 * obj-&gt;addNum(num);
 * double param_2 = obj-&gt;findMedian();
 */</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br></div></div><h3 id="_3-3-0-大小顶堆"><a href="#_3-3-0-大小顶堆" class="header-anchor">#</a> 3.3.0.大小顶堆</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//大顶堆</span>
priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> BigHeap<span class="token punctuation">;</span>
priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token punctuation">,</span>less<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> BigHeap<span class="token punctuation">;</span>

<span class="token comment">//小顶堆</span>
priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token punctuation">,</span> greater<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> SmallHeap<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="_3-3-1-『增』元素"><a href="#_3-3-1-『增』元素" class="header-anchor">#</a> 3.3.1.『增』元素</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>solve<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token keyword">int</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-3-2-『删』元素"><a href="#_3-3-2-『删』元素" class="header-anchor">#</a> 3.3.2.『删』元素</h3> <ul><li>删掉该顶堆的最值</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>solve<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-3-3-『改』元素"><a href="#_3-3-3-『改』元素" class="header-anchor">#</a> 3.3.3.『改』元素</h3> <ul><li>无</li></ul> <h3 id="_3-3-4-『查』元素"><a href="#_3-3-4-『查』元素" class="header-anchor">#</a> 3.3.4.『查』元素</h3> <p>查该顶堆的最值</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">return</span> solve<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_3-3-5-『元素个数』"><a href="#_3-3-5-『元素个数』" class="header-anchor">#</a> 3.3.5.『元素个数』</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>solve<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="✔️04-仿函数统一场理论"><a href="#✔️04-仿函数统一场理论" class="header-anchor">#</a> ✔️04.仿函数统一场理论</h2> <h3 id="_4-1-「自建函数」和「比较类-仿函数-」的统一场理论"><a href="#_4-1-「自建函数」和「比较类-仿函数-」的统一场理论" class="header-anchor">#</a> 4.1.「自建函数」和「比较类(仿函数)」的统一场理论</h3> <ul><li>STL中自建函数和比较类（仿函数）总结对比、统一</li></ul> <h3 id="_4-2-应用：自建比较priority-queue和sort"><a href="#_4-2-应用：自建比较priority-queue和sort" class="header-anchor">#</a> 4.2.应用：自建比较<code>priority_queue</code>和sort</h3> <ul><li><code>priority_queue</code>内元素优先级设置</li> <li>sort函数中优先级设定</li></ul> <h3 id="✔️4-3-priority-queue"><a href="#✔️4-3-priority-queue" class="header-anchor">#</a> ✔️4.3.<code>priority_queue</code></h3> <h3 id="⭐️4-3-1-难点（没搞懂）"><a href="#⭐️4-3-1-难点（没搞懂）" class="header-anchor">#</a> ⭐️4.3.1.难点（没搞懂）</h3> <ul><li><code>priority_queue</code> 优先队列中结构体的优先级设置</li> <li>如何理解结构体中overload的<code>&lt;</code>呢？
<ul><li>1、可以理解为重载后<code>小于号</code>作用是比较出数组中优先级更小的数据，如果返回的是某个数据值更大的一方则值越大优先级越小，相反则值越小优先级越大。</li> <li>2、优先队列是输出优先级最高的数据的队列。</li> <li>3、可以简单的记为与sort中的cmp函数效果相反。</li></ul></li></ul> <h3 id="⭐️4-3-2-基本数据类型"><a href="#⭐️4-3-2-基本数据类型" class="header-anchor">#</a> ⭐️4.3.2.基本数据类型</h3> <ul><li>int，double，char
<ul><li>优先队列对它们的优先级设置一般是<strong>数字大</strong>的<strong>优先级越高</strong>。</li></ul></li></ul> <p>比如下面2个是等价的（最大堆）</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> q<span class="token punctuation">;</span>
priority_queue<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token punctuation">,</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token punctuation">,</span>less<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> q<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>上面</p> <ul><li>⭐️第2个参数
<ul><li><code>vector&lt;int&gt;</code>填写的是来<strong>承载</strong>底层数据结构堆的<strong>容器</strong></li></ul></li> <li>⭐️第3个参数
是对第1个参数的<strong>比较类（是1个类）</strong></li></ul> <p>如果对于<strong>基本数据类型</strong>想要最小堆</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>priority_queue<span class="token operator">&lt;</span> <span class="token keyword">int</span> <span class="token punctuation">,</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token punctuation">,</span> greater<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span> q<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="⭐️4-3-3-结构体类型（重载-）"><a href="#⭐️4-3-3-结构体类型（重载-）" class="header-anchor">#</a> ⭐️4.3.3.结构体类型（重载&lt;）</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">struct</span> <span class="token class-name">fruit</span>
<span class="token punctuation">{</span>
	string name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> price<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="⭐️4-3-3-1-方法一"><a href="#⭐️4-3-3-1-方法一" class="header-anchor">#</a> ⭐️4.3.3.1.方法一</h3> <ul><li>下面这几句话很重要</li></ul> <blockquote><p>1.设置int的优先级</p></blockquote> <ul><li>这样<strong>小于&lt;号</strong>的作用还是<strong>小于&lt;号</strong>的作用，所以<code>priority_queue</code>默认还是<strong>最大堆</strong></li> <li>（此处虽然难理解，但是也算是我给自己的“记忆宫殿”<strong>设定的一种记忆逻辑</strong>）</li> <li>（PS：原因是，<strong>sort函数中cmp的设定就有点不一样</strong>）</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">struct</span> <span class="token class-name">fruit</span>
<span class="token punctuation">{</span>
	string name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> price<span class="token punctuation">;</span>
	<span class="token comment">//结构体增加一个，友元函数</span>
	<span class="token keyword">friend</span> <span class="token keyword">bool</span> <span class="token keyword">operator</span> <span class="token operator">&lt;</span> <span class="token punctuation">(</span>fruit f1<span class="token punctuation">,</span>fruit f2<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">return</span> f1<span class="token punctuation">.</span>price<span class="token operator">&lt;</span>f2<span class="token punctuation">.</span>price<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>2.设定string的优先级</p></blockquote> <ul><li>这样<strong>小于&lt;号</strong>的作用还是<strong>小于&lt;号</strong>的作用，所以<code>priority_queue</code>默认还是<strong>最大堆</strong></li> <li>表示，<strong>字典序大</strong>的优先级高？<strong>是的!</strong></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">struct</span> <span class="token class-name">fruit</span>
<span class="token punctuation">{</span>
	string name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> price<span class="token punctuation">;</span>
	<span class="token comment">//结构体增加一个，友元函数</span>
	<span class="token keyword">friend</span> <span class="token keyword">bool</span> <span class="token keyword">operator</span> <span class="token operator">&lt;</span> <span class="token punctuation">(</span>fruit f1<span class="token punctuation">,</span>fruit f2<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">return</span> f1<span class="token punctuation">.</span>name<span class="token operator">&lt;</span>f2<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>代码测试</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">struct</span> <span class="token class-name">fruit</span>
<span class="token punctuation">{</span>
	string name<span class="token punctuation">;</span>
	<span class="token keyword">int</span> price<span class="token punctuation">;</span>
	<span class="token comment">//结构体增加一个，友元函数</span>
	<span class="token keyword">friend</span> <span class="token keyword">bool</span> <span class="token keyword">operator</span> <span class="token operator">&lt;</span> <span class="token punctuation">(</span>fruit f1<span class="token punctuation">,</span>fruit f2<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">return</span> f1<span class="token punctuation">.</span>name<span class="token operator">&lt;</span>f2<span class="token punctuation">.</span>name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	priority_queue<span class="token operator">&lt;</span>fruit<span class="token operator">&gt;</span> q<span class="token punctuation">;</span>
	fruit t<span class="token punctuation">;</span>
	t<span class="token punctuation">.</span>name<span class="token operator">=</span><span class="token string">&quot;aaa&quot;</span><span class="token punctuation">;</span>
	t<span class="token punctuation">.</span>price<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
	
	q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	t<span class="token punctuation">.</span>name<span class="token operator">=</span><span class="token string">&quot;aa&quot;</span><span class="token punctuation">;</span>
	t<span class="token punctuation">.</span>price<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
	
	q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	t<span class="token punctuation">.</span>name<span class="token operator">=</span><span class="token string">&quot;aaaaa&quot;</span><span class="token punctuation">;</span>
	t<span class="token punctuation">.</span>price<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
	
	q<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">!</span>q<span class="token punctuation">.</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		cout<span class="token operator">&lt;&lt;</span>q<span class="token punctuation">.</span><span class="token function">top</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>name<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
		q<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><p>输出为</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>aaaaa
aaa
aa
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="⭐️4-3-3-2-方法二（比较类）"><a href="#⭐️4-3-3-2-方法二（比较类）" class="header-anchor">#</a> ⭐️4.3.3.2.方法二（比较类）</h3> <ul><li>有没有办法和sort中的cmp函数那样写在结构体外面呢？</li> <li>有的，写一个比较类</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">struct</span> <span class="token class-name">cmp</span>
<span class="token punctuation">{</span>
	<span class="token comment">//注意，类中的重载的是()</span>
	<span class="token keyword">bool</span> <span class="token keyword">operator</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>fruit f1<span class="token punctuation">,</span>fruit f2<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">return</span> f1<span class="token punctuation">.</span>price <span class="token operator">&gt;</span> f2<span class="token punctuation">.</span>price<span class="token punctuation">;</span> 
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>使用如下</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>priority_queue<span class="token operator">&lt;</span> fruit <span class="token punctuation">,</span> vector<span class="token operator">&lt;</span>fruit<span class="token operator">&gt;</span><span class="token punctuation">,</span> cmp<span class="token operator">&gt;</span> q<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h3 id="_4-4-sort函数"><a href="#_4-4-sort函数" class="header-anchor">#</a> 4.4.sort函数</h3> <h3 id="_4-4-1-尝试统一sort和-priority-queue"><a href="#_4-4-1-尝试统一sort和-priority-queue" class="header-anchor">#</a> 4.4.1.尝试统一<code>sort</code>和 <code>priority_queue</code></h3> <ul><li><p>//从大到小排序</p></li> <li><p>//怎么说呢，这个和<code>priority_queue</code>中的设定看起来有些相反</p></li> <li><p>1.我尝试合并一下想法，假设，priority_queue的底层也是vector承载
那么，最大堆，也就是<code>&lt;</code>默认重载是一样的，那么建立堆之后，我们的最大值也就在堆顶。</p></li> <li><p>2.这个时候，我们把STL中的<strong>make_heap</strong>扯进来，它在vector上面建堆，就是这样子的，最大的在最后。</p></li> <li><p>3.所以，底层的话，这个比较是统一的</p></li> <li><p>4.我们都是重载<code>&lt;</code>，但是<code>priority_queue</code>取数字是<strong>从后面往前面</strong>取。
我们<strong>sort</strong>后是喜欢<strong>从前面往后面</strong>遍历，自然会出现反常的感觉。</p></li></ul> <h3 id="_4-4-2-方法（建比较函数）"><a href="#_4-4-2-方法（建比较函数）" class="header-anchor">#</a> 4.4.2.方法（建比较函数）</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">bool</span> <span class="token function">cmp</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">,</span><span class="token keyword">int</span> b<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">return</span> a<span class="token operator">&gt;</span>b<span class="token punctuation">;</span>	
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>2、结构体数据类型</p> <h3 id="_4-5-stl中可以放仿函数的地方也可以放普通函数？"><a href="#_4-5-stl中可以放仿函数的地方也可以放普通函数？" class="header-anchor">#</a> 4.5.STL中可以放仿函数的地方也可以放普通函数？</h3> <h4 id="_4-5-1-仿函数和普通函数都能在这个地方使用"><a href="#_4-5-1-仿函数和普通函数都能在这个地方使用" class="header-anchor">#</a> 4.5.1.仿函数和普通函数都能在这个地方使用</h4> <p>Tips：struct和class修饰都OK啦，在这个方面（仿函数的话）</p> <ul><li><a href="http://www.cplusplus.com/reference/algorithm/sort/?kw=sort" target="_blank" rel="noopener noreferrer">sort的官方文档<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">RandomAccessIterator</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">Compare</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">sort</span> <span class="token punctuation">(</span>RandomAccessIterator first<span class="token punctuation">,</span> RandomAccessIterator last<span class="token punctuation">,</span> Compare comp<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li><a href="http://www.cplusplus.com/reference/numeric/accumulate/?kw=accumulate" target="_blank" rel="noopener noreferrer">accumulate的官方文档<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">InputIterator</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">T</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">BinaryOperation</span><span class="token operator">&gt;</span>
   T <span class="token function">accumulate</span> <span class="token punctuation">(</span>InputIterator first<span class="token punctuation">,</span> InputIterator last<span class="token punctuation">,</span> T init<span class="token punctuation">,</span>
                 BinaryOperation binary_op<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>其实统一是：（强调仿函数<strong>Pred pr</strong>）</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">RandomAccessIterator</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">Compare</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span> <span class="token function">sort</span> <span class="token punctuation">(</span>RandomAccessIterator first<span class="token punctuation">,</span> RandomAccessIterator last<span class="token punctuation">,</span> Pred pr<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>和</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span> <span class="token operator">&lt;</span><span class="token keyword">class</span> <span class="token class-name">InputIterator</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">T</span><span class="token punctuation">,</span> <span class="token keyword">class</span> <span class="token class-name">BinaryOperation</span><span class="token operator">&gt;</span>
   T <span class="token function">accumulate</span> <span class="token punctuation">(</span>InputIterator first<span class="token punctuation">,</span> InputIterator last<span class="token punctuation">,</span> T init<span class="token punctuation">,</span>
                 Pred pr<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="_4-5-2-sort官方文档这么使用"><a href="#_4-5-2-sort官方文档这么使用" class="header-anchor">#</a> 4.5.2.sort官方文档这么使用</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">// sort algorithm example</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span>     <span class="token comment">// std::cout</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;algorithm&gt;</span>    <span class="token comment">// std::sort</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;vector&gt;</span>       <span class="token comment">// std::vector</span></span>

<span class="token comment">//普通自定义函数</span>
<span class="token keyword">bool</span> <span class="token function">myfunction</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">,</span><span class="token keyword">int</span> j<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">(</span>i<span class="token operator">&lt;</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>

<span class="token comment">//重载类的()运算符，弄出仿函数</span>
<span class="token keyword">struct</span> <span class="token class-name">myclass</span> <span class="token punctuation">{</span>
  <span class="token keyword">bool</span> <span class="token keyword">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token punctuation">,</span><span class="token keyword">int</span> j<span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token punctuation">(</span>i<span class="token operator">&lt;</span>j<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span> myobject<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">int</span> myints<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">32</span><span class="token punctuation">,</span><span class="token number">71</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">45</span><span class="token punctuation">,</span><span class="token number">26</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">,</span><span class="token number">53</span><span class="token punctuation">,</span><span class="token number">33</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    
  <span class="token comment">// 32 71 12 45 26 80 53 33</span>
  std<span class="token operator">::</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">myvector</span> <span class="token punctuation">(</span>myints<span class="token punctuation">,</span> myints<span class="token operator">+</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>               

  <span class="token comment">// using default comparison (operator &lt;):</span>
  <span class="token comment">//(12 32 45 71)26 80 53 33</span>
  std<span class="token operator">::</span><span class="token function">sort</span> <span class="token punctuation">(</span>myvector<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> myvector<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           

  <span class="token comment">// using function as comp，用普通函数作为比较操作</span>
  <span class="token comment">// 12 32 45 71(26 33 53 80)</span>
  std<span class="token operator">::</span><span class="token function">sort</span> <span class="token punctuation">(</span>myvector<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">4</span><span class="token punctuation">,</span> myvector<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> myfunction<span class="token punctuation">)</span><span class="token punctuation">;</span> 

  <span class="token comment">// using object as comp，用“类生产的对象”来作为比较操作</span>
  <span class="token comment">//(12 26 32 33 45 53 71 80)</span>
  std<span class="token operator">::</span><span class="token function">sort</span> <span class="token punctuation">(</span>myvector<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> myvector<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> myobject<span class="token punctuation">)</span><span class="token punctuation">;</span>     
	
  <span class="token comment">// print out content:</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;myvector contains:&quot;</span><span class="token punctuation">;</span>
  <span class="token keyword">for</span> <span class="token punctuation">(</span>std<span class="token operator">::</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">::</span>iterator it<span class="token operator">=</span>myvector<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> it<span class="token operator">!=</span>myvector<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token operator">++</span>it<span class="token punctuation">)</span>
    std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">' '</span> <span class="token operator">&lt;&lt;</span> <span class="token operator">*</span>it<span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">'\n'</span><span class="token punctuation">;</span>

  <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><h4 id="_4-5-3-accumulate的两种使用方式"><a href="#_4-5-3-accumulate的两种使用方式" class="header-anchor">#</a> 4.5.3.accumulate的两种使用方式</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">// accumulate example</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span>     <span class="token comment">// std::cout</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;functional&gt;</span>   <span class="token comment">// std::minus</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;numeric&gt;</span>      <span class="token comment">// std::accumulate</span></span>

<span class="token comment">//函数</span>
<span class="token keyword">int</span> <span class="token function">myfunction</span> <span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> x<span class="token operator">+</span><span class="token number">2</span><span class="token operator">*</span>y<span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token comment">//仿函数</span>
<span class="token keyword">struct</span> <span class="token class-name">myclass</span> <span class="token punctuation">{</span>
	<span class="token keyword">int</span> <span class="token keyword">operator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token keyword">int</span> x<span class="token punctuation">,</span> <span class="token keyword">int</span> y<span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token keyword">return</span> x<span class="token operator">+</span><span class="token number">3</span><span class="token operator">*</span>y<span class="token punctuation">;</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span> myobject<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">int</span> init <span class="token operator">=</span> <span class="token number">100</span><span class="token punctuation">;</span>
  <span class="token keyword">int</span> numbers<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">20</span><span class="token punctuation">,</span><span class="token number">30</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;using default accumulate: &quot;</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> std<span class="token operator">::</span><span class="token function">accumulate</span><span class="token punctuation">(</span>numbers<span class="token punctuation">,</span>numbers<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">,</span>init<span class="token punctuation">)</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">'\n'</span><span class="token punctuation">;</span>

  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;using functional's minus: &quot;</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> std<span class="token operator">::</span><span class="token function">accumulate</span> <span class="token punctuation">(</span>numbers<span class="token punctuation">,</span> numbers<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">,</span> init<span class="token punctuation">,</span> std<span class="token operator">::</span>minus<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">'\n'</span><span class="token punctuation">;</span>

    <span class="token comment">//使用用户自定义函数</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;using custom function: &quot;</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> std<span class="token operator">::</span><span class="token function">accumulate</span> <span class="token punctuation">(</span>numbers<span class="token punctuation">,</span> numbers<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">,</span> init<span class="token punctuation">,</span> myfunction<span class="token punctuation">)</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">'\n'</span><span class="token punctuation">;</span>

<span class="token comment">//使用对象</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;using custom object: &quot;</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> std<span class="token operator">::</span><span class="token function">accumulate</span> <span class="token punctuation">(</span>numbers<span class="token punctuation">,</span> numbers<span class="token operator">+</span><span class="token number">3</span><span class="token punctuation">,</span> init<span class="token punctuation">,</span> myobject<span class="token punctuation">)</span><span class="token punctuation">;</span>
  std<span class="token operator">::</span>cout <span class="token operator">&lt;&lt;</span> <span class="token string">'\n'</span><span class="token punctuation">;</span>

  <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br></div></div><h2 id="_05-string剖析c"><a href="#_05-string剖析c" class="header-anchor">#</a> 05.<code>string</code>剖析C++</h2> <h3 id="_5-1源代码来源"><a href="#_5-1源代码来源" class="header-anchor">#</a> 5.1源代码来源</h3> <ul><li>DevC++5.9.2中的</li> <li>TDM-GCC 4.8.1</li> <li>如果没有安装DevC++可以移步网页源代码版本</li> <li><a href="https://gcc.gnu.org/onlinedocs/gcc-4.9.0/libstdc++/api/a00999_source.html" target="_blank" rel="noopener noreferrer">libstdc++在线文档：传送门<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h3 id="_5-2string的使用接口"><a href="#_5-2string的使用接口" class="header-anchor">#</a> 5.2string的使用接口</h3> <ul><li><a href="http://www.cplusplus.com/reference/string/string/?kw=string" target="_blank" rel="noopener noreferrer">c plusplus中string使用传送门<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h3 id="_5-3bits-basic-string-h头文件"><a href="#_5-3bits-basic-string-h头文件" class="header-anchor">#</a> 5.3<code>bits/basic_string.h</code>头文件</h3> <ul><li><p>如果直接去看源码，会比较难理解，因为basic_string的实现使用了COPY-ON-WRITE技术。</p></li> <li><p>关于COPY-ON-WRITE技术可移步</p></li> <li><p>写时拷贝技术(copy-on-write)</p></li> <li><p>STL之basic_string好的<a href="https://blog.csdn.net/liuyuan185442111/article/details/45870441" target="_blank" rel="noopener noreferrer">CSDN文章传送门<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li> <li><p>参考文章：Linux<a href="https://www.cnblogs.com/lengender-12/p/7054896.html" target="_blank" rel="noopener noreferrer">写时拷贝技术<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li></ul> <h2 id="参考资料"><a href="#参考资料" class="header-anchor">#</a> 参考资料</h2> <ul><li><p>网站<a href="http://www.cplusplus.com/reference/string/string/?kw=string" target="_blank" rel="noopener noreferrer">C plusplus<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li> <li><p>配套习题：来自我的『牛客博客』</p> <ul><li>用于查看声明，比VS系列编译器的简洁明了一些，还有示例程序</li></ul></li> <li><p>1、<a href="http://www.cplusplus.com/reference/" target="_blank" rel="noopener noreferrer">cplusplus.com（我常用这个）<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li> <li><p>2、<a href="https://zh.cppreference.com/w/%E9%A6%96%E9%A1%B5" target="_blank" rel="noopener noreferrer">zh.cppreference（看C++11常用）<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li></ul></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/27.13d60edd.js" defer></script>
  </body>
</html>
